package com.kzw.core.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.databind.JavaType;
import com.kzw.core.mapper.JsonMapper;
import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;

public class ExtjsUtil {

	/**
	 * Ext从Request请求中获得分页信息，Ext.Grid传递过来的是start, limit参数
	 * */
	public static PageRequest getPageReq(HttpServletRequest request) {
		
		int pageSize = Integer.parseInt(StringUtils.defaultIfEmpty(request
				.getParameter("limit"), "25"));
		int pageNo = Integer.parseInt(StringUtils.defaultIfEmpty(request
				.getParameter("start"), "0"))/pageSize + 1;
		
		String sort = request.getParameter("sort");
		JavaType javaType = JsonMapper.nonDefaultMapper().contructCollectionType(List.class, Map.class);
		List<Map<String, String>> list = JsonMapper.nonDefaultMapper().fromJson(sort, javaType);
		
		PageRequest pageRequest = new PageRequest(pageNo, pageSize);
		if(sort!=null && list.size()>0) {
			List<String> sorts = new ArrayList<String>();
			List<String> dirs = new ArrayList<String>();
			for(Map<String, String> map : list) {
				sorts.add(map.get("property"));
				dirs.add(map.get("direction"));
			}
			pageRequest.setOrderBy(StringUtils.join(sorts, ","));
			pageRequest.setOrderDir(StringUtils.join(dirs, ","));
		}
		
		return pageRequest;
	}
	
	/**
	 * Ext从Request请求中获得属性字段过滤集，用于查询
	 * */
	public static List<StringPropertyFilter> getFilters(HttpServletRequest request) {
		return StringPropertyFilter.buildFromHttpRequest(request);
	}
	
	/**
	 * 是否导出结果
	 * */
	public static boolean isExport(HttpServletRequest request) {
		//是否导出
		boolean isExport=false;
		if("true".equals(request.getParameter("isExport"))){
    		isExport=true;
    		request.setAttribute("colId", request.getParameter("colId"));
			request.setAttribute("colName", request.getParameter("colName"));
			request.setAttribute("exportType", request.getParameter("exportType"));
			
			SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddhhmmssSSS"); 
    		String datetime = tempDate.format(new java.util.Date());
    		request.setAttribute("fileName", datetime);
    	}
		request.setAttribute("isExport", isExport);
		return isExport;
	}
	
	/**
	 * 构造分页信息
	 * */
	public static <T> Page<T> toPageForExtJs(PageRequest pageRequest, List<T> list) {
		Page<T> page = new Page<T>(pageRequest);
		page.setTotalCounts(list.size());
		int endIndex =  page.getOffset()+page.getPageSize();
		if(endIndex > list.size()) {
			endIndex = list.size();
		}
		if (page.getOffset()>endIndex) {
			page.setPageNo(1);
		}
		List<T> subList = list.subList(page.getOffset(), endIndex);
		page.setResult(subList);
		return page;
	}
	
	/**
	 * 从过滤条件中获得HQL片段
	 * */
	public static String getHqlFromFilter(List<StringPropertyFilter> filters, List<Object> params, String prefix) {
		String p = prefix + ".";
		if(prefix == null) {
			p = "";
		}
		
		List<String> list = new ArrayList<String>();
		for(StringPropertyFilter pf : filters) {
			switch (pf.getMatchType()) {
			case EQ:
				list.add(p+pf.getPropertyName()+"=?");
				params.add(pf.getMatchValue());
				break;
			case NE:
				list.add(p+pf.getPropertyName()+"!=?");
				params.add(pf.getMatchValue());
				break;
			case LE:
				list.add(p+pf.getPropertyName()+" <=?");
				params.add(pf.getMatchValue());
				break;
			case LT:
				list.add(p+pf.getPropertyName()+" <?");
				params.add(pf.getMatchValue());
				break;
			case GE:
				list.add(p+pf.getPropertyName()+" >=?");
				params.add(pf.getMatchValue());
				break;
			case GT:
				list.add(p+pf.getPropertyName()+" >?");
				params.add(pf.getMatchValue());
				break;
			case LIKE:
				list.add(p+pf.getPropertyName()+" like ?");
				params.add("%"+pf.getMatchValue()+"%");
				break;
			case IN:
				list.add(p+pf.getPropertyName()+" in (?)");
				params.add(pf.getMatchValue());
				break;
			case NULL:
				list.add(p+pf.getPropertyName()+" is null");
				break;
			case NOTNULL:
				list.add(p+pf.getPropertyName()+" is not null");
				break;
			default:
				break;
			}
		}
		
		if(list.size() == 0) return " 1=1 and ";
		
		StringBuffer sb = new StringBuffer(" ");
		for(String str : list) {
			sb.append(str).append(" and ");
		}
		//String hql = sb.substring(0, sb.length()-5);
		return sb.toString();
		
	}
	
	/**
	 * @param filters 从请求中获得的StringPropertyFilter集合
	 * @param params 输出参数，在外部new ArrayList<>()传入
	 * */
	public static String getHqlFromFilter(List<StringPropertyFilter> filters, List<Object> params) {
		return getHqlFromFilter(filters, params, null);
	}
}
